package cn.shenlong.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import cn.shenlong.common.Constants;
import cn.shenlong.portal.domain.EmailConfig;

public class MailUtil {
	
	static {
		Properties property = new Properties();
		try {
			property.load(new FileInputStream(Constants.APPLICATION_URL+"..\\config\\global.properties"));
			String sPort = property.getProperty("email.port");
			port = Integer.valueOf(sPort);
			server = property.getProperty("email.server");
			from = property.getProperty("email.from");
			user = property.getProperty("email.user");
			password = property.getProperty("email.password");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static int port = 25; // smtp端口

	private static String server = "smtp.163.com"; // smtp服务器地址

	private static String from = "lunaxyz@163.com"; // 发送者

	private static String user = "lunaxyz@163.com";// 发送者地址

	private static String password = "abc123";// 密码

	public static void sendEmail(String email, String subject, String body) {
		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", server);
			props.put("mail.smtp.port", String.valueOf(port));
			props.put("mail.smtp.auth", "true");
			Transport transport = null;
			Session session = Session.getDefaultInstance(props, null);
			transport = session.getTransport("smtp");
			transport.connect(server, user, password);
			MimeMessage msg = new MimeMessage(session);
			msg.setSentDate(new Date());
			InternetAddress fromAddress = new InternetAddress(from);
			msg.setFrom(fromAddress);
			InternetAddress[] toAddress = new InternetAddress[1];
			toAddress[0] = new InternetAddress(email);
			msg.setRecipients(Message.RecipientType.TO, toAddress);
			msg.setSubject(subject, "UTF-8");
			msg.setText(body, "UTF-8");
			msg.saveChanges();
			transport.sendMessage(msg, msg.getAllRecipients());
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendEmail(String email, String subject, String body,EmailConfig emailConfig) {
		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", emailConfig.getEmailServer());
			props.put("mail.smtp.port", String.valueOf(emailConfig.getEmailPort()));
			props.put("mail.smtp.auth", "true");
			Transport transport = null;
			Session session = Session.getDefaultInstance(props, null);
			transport = session.getTransport("smtp");
			transport.connect(emailConfig.getEmailServer(), emailConfig.getUser(), emailConfig.getPassword());
			MimeMessage msg = new MimeMessage(session);
			msg.setSentDate(new Date());
			InternetAddress fromAddress = new InternetAddress(emailConfig.getEmailFrom());
			msg.setFrom(fromAddress);
			InternetAddress[] toAddress = new InternetAddress[1];
			toAddress[0] = new InternetAddress(email);
			msg.setRecipients(Message.RecipientType.TO, toAddress);
			msg.setSubject(subject, "UTF-8");
			msg.setText(body, "UTF-8");
			msg.saveChanges();
			transport.sendMessage(msg, msg.getAllRecipients());
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}
